#include "StdAfx.h"
#include "CylinderBuilder.h"
#include <BRepPrimAPI_MakeCylinder.hxx> 
#include "VarList.h"
#include "labelTools.h"
#include "ShapeInfoFactory.h"

REGISTERE_SHAPEINFO(CCylinderInfo, CYLINDER_GUID);
//////////////////////////////////////////////////////////////////////////
CCylinderInfo::CCylinderInfo():dRadius(0)
{
	m_szGUID = CYLINDER_GUID;
}
CCylinderInfo::~CCylinderInfo()
{
}
void CCylinderInfo::Reset()
{
	dRadius = 0;
	dHeight = 1.0;
	dir = gp_Dir(0,0,1);
}
void CCylinderInfo::GetParams(CVarList& varList, UINT nID)
{
	varList.SetID(CYLINDER_GUID);
	varList.SetValue(CENTER_POINT, ptCenter);
	varList.SetValue(RADIUS, dRadius);
	varList.SetValue(DIRECTION, dir);
	varList.SetValue(HEIGHT, dHeight);
}
void CCylinderInfo::SetParams(const CVarList& varList, UINT nID)
{
	CPointProxy point;
	varList.GetValue(CENTER_POINT, point);
	ptCenter = point;
	varList.GetValue(RADIUS, dRadius);
	varList.GetValue(DIRECTION, point);
	dir = point;
	varList.GetValue(HEIGHT, dHeight);
}
bool CCylinderInfo::GetFromLabel(TDF_Label& label)
{
	TDF_Label childLabel;
	if(!LabelTools::GetChild(label, CENTER_POINT, ptCenter, childLabel))
		return false;

	if(!LabelTools::GetChild(label, RADIUS, dRadius, childLabel))
		return false;

	if(!LabelTools::GetChild(label, DIRECTION, dir, childLabel))
		return false;

	if(!LabelTools::GetChild(label, HEIGHT, dHeight, childLabel))
		return false;

	return true;
}

void CCylinderInfo::AddToLabel(TDF_Label& label)
{
	LabelTools::SetChild(label, CENTER_POINT, ptCenter);
	LabelTools::SetChild(label, RADIUS, dRadius);
	LabelTools::SetChild(label, DIRECTION, dir);
	LabelTools::SetChild(label, HEIGHT, dHeight);
}

bool CCylinderInfo::ModifyLabel(TDF_Label& label, TFunction_Logbook& log)
{
	if(!LabelTools::v())
		return false;

	if(!LabelTools::UpdateChild(label, RADIUS, dRadius, log))
		return false;

	if(!LabelTools::UpdateChild(label, DIRECTION, dir, log))
		return false;

	if(!LabelTools::UpdateChild(label, HEIGHT, dHeight, log))
		return false;

	return true;
}

bool CCylinderInfo::IsModified(TDF_Label& label,const TFunction_Logbook& log)
{
	if(log.IsModified(label.FindChild(CENTER_POINT)))
		return true; 

	if(log.IsModified(label.FindChild(RADIUS)))
		return true; 

	if(log.IsModified(label.FindChild(DIRECTION)))
		return true; 

	if(log.IsModified(label.FindChild(HEIGHT)))
		return true; 

	return false;
}
//////////////////////////////////////////////////////////////////////////
CCylinderBuilder::CCylinderBuilder(void)
{
}

CCylinderBuilder::~CCylinderBuilder(void)
{
}

bool CCylinderBuilder::Build(const CString& szCmdParam, TopoDS_Shape& shape)
{
	return false;
}

bool CCylinderBuilder::Build(CCylinderInfo* pShapeInfo, TopoDS_Shape& shape)
{
	if(pShapeInfo->dRadius < 0.1)
		pShapeInfo->dRadius = 0.1;
	if(pShapeInfo->dHeight < 1)
		pShapeInfo->dHeight = 1;
	try
	{
		gp_Ax2 ax2(pShapeInfo->ptCenter, pShapeInfo->dir);
		BRepPrimAPI_MakeCylinder mkCyl( ax2, pShapeInfo->dRadius, pShapeInfo->dHeight);
		shape = mkCyl.Shape();
	}
	catch (...) 
	{
		return false;
	}

	return true;
}